package classes{
	import beniAIRLib.filesystem.BeniFile;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class CreateStructure{
		
		private var rootFolder:File;
		private var packageFolder:File;
		private var targetFolder:File;
		private var targetPackageFolder:File;
		
		private var packageName:String;
		private var appName:String;
		
		private var modelLocator:ModelLocator = ModelLocator.item;
		
		
		public function CreateStructure(isWebApp:Boolean){
			var path:String = modelLocator.path;
			var packageName:String = modelLocator.packageName;
			var appName:String = modelLocator.appName;

			this.packageName = packageName;
			this.appName = appName;
			
			rootFolder = File.applicationDirectory.resolvePath('template/#Root/');
			packageFolder = File.applicationDirectory.resolvePath('template/#Package/');
			
			targetFolder = new File(modelLocator.sourceFolder);
			targetPackageFolder = new File(modelLocator.packageFolder);
			
			rootFolder.copyTo(targetFolder, true);
			packageFolder.copyTo(targetPackageFolder, true);
			
			
			
			
			
			// handle air application
			if (!isWebApp){
				var airFolder:File = File.applicationDirectory.resolvePath('template/#AirOnly/');
				var airTargetFolder:File;
				var childrenAry:Array = airFolder.getDirectoryListing();
				for each(var tempFile:File in childrenAry){
					airTargetFolder = new File(targetFolder.nativePath + '//' + airFolder.getRelativePath(tempFile));
					tempFile.copyTo(airTargetFolder, true);
				}
			}


			
			
			// get all files
			var files:Array = BeniFile.getAllfiles(targetFolder);
			var fileItem:File;
			
			
			for each (fileItem in files){
				updateContent(fileItem);
				fileItem = updateFileName(fileItem);
				var fileName:String = fileItem.name;
				if(fileName.charAt(fileName.length-1) == '_'){
					fileName = fileName.substr(0, fileName.length-1);
					BeniFile.rename(fileItem, fileName);
				}
			}
			files = BeniFile.getAllfiles(targetFolder);
			// clear files
			for each (fileItem in files){
				if (fileItem.name.indexOf('.temp') > -1){
					fileItem.deleteFile();
				}
			}
			
			// end..
			
			
			
		}
		
		
		private function updateContent(file:File):void{
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var str:String = UtilFun.getReplaceStr(fileStream.readUTFBytes(fileStream.bytesAvailable));
			fileStream.close();
			fileStream.open(file, FileMode.UPDATE);
			fileStream.position = 0;
			fileStream.writeUTFBytes(str);
			fileStream.truncate();
			fileStream.close();
		}
		private function updateFileName(file:File):File{
			var result:File = file;
			var oldFileName:String = file.nativePath;
			var newFileName:String = UtilFun.getReplaceStr(oldFileName);
			var newFile:File;
			if (oldFileName != newFileName){
				newFile = new File(newFileName);
				file.moveTo(newFile);
				result = newFile;
			}
			return result;
		}
		
	}
}